import { getInfo, login, getuser } from '@/api/user'
import { Message } from 'element-ui'
// import { resetRouter } from '@/router'

const state = {
  token: localStorage.getItem('token') || '',
  userInfo: {}
}
const mutations = {
  getout(state) {
    state.token = ''
    localStorage.removeItem('token')
    state.userInfo = {}
    // resetRouter()
  },
  settoken(state, str) {
    console.log('代码执行到登入')
    Message.success('登录成功')
    state.token = str
    localStorage.setItem('token', str)
  },
  setUserInfo(state, data) {
    state.userInfo = data
  }
}
const actions = {
  async login(store, data) {
    const res = await login(data)
    console.log('执行成功')
    store.commit('settoken', res)
  },
  async getUserInfo(store) {
    const res = await getInfo()
    const res2 = await getuser(res.userId)
    console.log({
      ...res,
      ...res2
    })
    store.commit('setUserInfo', { ...res, ...res2 })
    return {
      ...res,
      ...res2
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
